package com.giscloud.amp.service.regis.impl;

import com.alibaba.fastjson.JSON;
import com.giscloud.amp.core.ServiceConstConfig;
import com.giscloud.amp.dao.AreaMapper;
import com.giscloud.amp.entity.Area;
import com.giscloud.amp.entity.AreaExample;
import com.giscloud.amp.entity.regis.dto.AreaDTO;
import com.giscloud.amp.service.regis.AreaService;
import com.giscloud.datasource.page.PageInfo;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author:gxd
 * @Description: 权限服务类
 * @Date: 17:19 2018/2/24
 * @Modified By:
 */
@Service("areaService")
public class AreaServiceImpl implements AreaService {

    private final static Logger log = LoggerFactory.getLogger(AreaServiceImpl.class);

    @Autowired
    private ServiceConstConfig commConstConfig;

    @Autowired
    private AreaMapper areaMapper;

    /**
     * 根据名称获取行政区域,分页
     * @return
     */
    @Override
    public PageInfo findPageAreaByName(AreaDTO areaDTO) {
        PageHelper.startPage(areaDTO.getPage(), areaDTO.getRows());
        AreaExample areaExample = new AreaExample();
        AreaExample.Criteria criteria = areaExample.createCriteria();
        List<Integer> levels = new ArrayList<>();
        levels.add(0);
        levels.add(2);
        criteria.andAreaLevelIn(levels);
        if(areaDTO.getName()!=null){
            criteria.andNameLike("%"+areaDTO.getName()+"%");
        }
        areaExample.setOrderByClause("AREA_ID ASC");
        List<Area> list = areaMapper.selectByExample(areaExample);
        return PageInfo.of(list);
    }

    /**
     * 获取城市列表,额外两值 gatherMap 采集资源数据, regisMap 楼盘资源数据
     * @return
     */
    @Override
    public PageInfo findAreaMapByName(AreaDTO areaDTO) {
        log.debug("#findAreaMapByName parameter ={}", JSON.toJSONString(areaDTO));
        String param = "page="+areaDTO.getPage()+"&rows="+areaDTO.getRows()+"&areaLevel="+
                areaDTO.getAreaLevel()+"&name="+areaDTO.getName();
//        String result = HttpRequest.sendPost(commConstConfig.getNewdcUrl()+"regisAmpApi/findAreaMapByName.do",param);
//        Gson gson = new Gson();
//        Pagination pagination = gson.fromJson(result,Pagination.class);
        return PageInfo.of(new ArrayList<>());
    }
    @Override
    public List<Area> findAreaList(AreaDTO areaDTO) {
        AreaExample areaExample = new AreaExample();
        AreaExample.Criteria criteria = areaExample.createCriteria();
        if(StringUtils.isNotEmpty(areaDTO.getName())) {
            criteria.andNameLike("%" + areaDTO.getName() + "%");
        }
        criteria.andAreaLevelEqualTo(areaDTO.getAreaLevel());
        //数据级别
        if(areaDTO.getAreaLevel() == 1){//省份
            if(areaDTO.getAreaId() != null){
                criteria.andAreaIdEqualTo(areaDTO.getAreaId());
            }
        }else if(areaDTO.getAreaLevel() == 2){//市
            formatCondition(criteria,areaDTO);
            if(areaDTO.getParentArea() != null){
                criteria.andParentAreaEqualTo(areaDTO.getParentArea());
            }
            if(areaDTO.getAreaId() != null){
                criteria.andAreaIdEqualTo(areaDTO.getAreaId());
            }
        }else if(areaDTO.getAreaLevel() == 3){//行政区
            if(areaDTO.getParentArea() != null){
                criteria.andParentAreaEqualTo(areaDTO.getParentArea());
            }
            if(areaDTO.getAreaId() != null){
                criteria.andAreaIdEqualTo(areaDTO.getAreaId());
            }
        }else if(areaDTO.getAreaLevel() == 4){//街道办
            if(areaDTO.getParentArea() != null){
                criteria.andParentAreaEqualTo(areaDTO.getParentArea());
            }
        }
        criteria.andStatusCdEqualTo(1);
        return areaMapper.selectByExample(areaExample);
    }

    @Override
    public List<Area> listArea(AreaDTO areaDTO) {
        if(areaDTO.getAreaLevel() != null && areaDTO.getAreaLevel() == 1){
            AreaExample example = new AreaExample();
            example.createCriteria().andAreaLevelEqualTo(1).andStatusCdEqualTo(1);
            return areaMapper.selectByExample(example);
        }else{
            AreaExample example = new AreaExample();
            example.createCriteria().andParentAreaEqualTo(areaDTO.getAreaId()).andStatusCdEqualTo(1);
            return areaMapper.selectByExample(example);
        }
    }

    public void formatCondition(AreaExample.Criteria criteria,AreaDTO areaDTO){
        switch (areaDTO.getDataType()){
            case "1":
                criteria.andIfFineQualityNotEqualTo((short) 0);
                break;
            case "2":
                criteria.andIfFineQualityEqualTo((short) 0);
                break;
            case "3":
                criteria.andIfProvinceEqualTo((short) 1);
                break;
            case "4":
                criteria.andIfHotEqualTo((short) 1);
                break;
        }
    }

}
